package com.situ.mybatis.utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
	static SqlSessionFactory sessionFactory;
	
	static {
		InputStream inputStream = null;
		try {
			// 1. 获取 mybatis.xml(输入流 )
			inputStream = Resources.getResourceAsStream("mybatis.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 2.Sql会话管理仓库   获取   输入流(mybatis.xml)  [ mybatis.xml放到会话仓库 ]
		sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	
	public static SqlSession getSqlSession(){
		// 3.   sql会话     拿到   会话仓库里的内容     [sql会话拿到"mybatis.xml"]
		SqlSession sqlSession = sessionFactory.openSession();
		return sqlSession;
	}
}
